package com.ecar.api.rest;

import com.ecar.common.datetime.TimestampMorpher;
import com.ecar.common.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** @noinspection unused */
public abstract class AbstractResource {

	protected static Logger log = LoggerFactory.getLogger(AbstractResource.class);

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected JSONObject _jsonParam = new JSONObject();

	protected JSONObject params = null;

	protected JSONObject fields = null;

	protected JSONArray datalist = null;

	protected JSONArray array = null;

	protected Integer id = null;

	protected String action = "";

	public String getAction() {
		return action;
	}

	public JSONObject getParams() {
		return params;
	}

	public void setParams(JSONObject params) {
		this.params = params;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public AbstractResource() {
		String[] formats = {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"};
		JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats));
	}

	protected void parseParams() {
		if (_jsonParam.get("params") != null)
			params = ((JSONObject) _jsonParam.get("params"));
		if (_jsonParam.get("fields") != null)
			fields = ((JSONObject) _jsonParam.get("fields"));
		if (_jsonParam.get("datalist") != null &&
			_jsonParam.get("datalist").getClass().equals(JSONArray.class))
			datalist = ((JSONArray) _jsonParam.get("datalist"));
		/*
		if (_jsonParam.get("array") != null &&
			_jsonParam.get("array").getClass().equals(JSONArray.class))
			array = ((JSONArray) _jsonParam.get("array"));
		if (_jsonParam.get("id") != null && StringUtil.isNotEmpty((String) _jsonParam.get("id")))
			id = ((Integer) _jsonParam.get("id"));
		if (_jsonParam.get("action") != null)
			action = ((String) _jsonParam.get("action"));
		*/
	}

	protected boolean parse(JSONObject param) {
		try {
			log.debug(param.toString());
			JSONObject params = param.getJSONObject("param");
			if (params == null) return false;

			if (params.containsKey("params"))
				this.params = (JSONObject) params.get("params");
			else
				this.params = null;

			if (params.containsKey("fields"))
				this.fields = (JSONObject) params.get("fields");
			else
				this.fields = null;

			if (params.containsKey("datalist"))
				this.datalist = (JSONArray) params.get("datalist");
			else
				this.datalist = null;
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	protected void parseParams(HttpServletRequest request) {
		try {
			String paramStr = request.getParameter("param");
			log.debug(paramStr);
			if (StringUtil.isNotEmpty(paramStr)) {
				this._jsonParam = JSONObject.fromObject(paramStr);
				parseParams();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
